{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "449e8234-2705-464b-9bbb-8ffb802432c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "single positional indexer is out-of-bounds",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[28], line 6\u001b[0m\n\u001b[0;32m      4\u001b[0m data \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mread_csv(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mD:/Users/19202/Desktop/highschool.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      5\u001b[0m j3_scores \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39miloc[:, \u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m----> 6\u001b[0m s1_scores \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39miloc[:, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m#计算 Pearson 相关系数\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mscipy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mstats\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pearsonr\n",
      "File \u001b[1;32mC:\\Anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1184\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   1182\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_is_scalar_access(key):\n\u001b[0;32m   1183\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_value(\u001b[38;5;241m*\u001b[39mkey, takeable\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_takeable)\n\u001b[1;32m-> 1184\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_tuple(key)\n\u001b[0;32m   1185\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   1186\u001b[0m     \u001b[38;5;66;03m# we by definition only have the 0th axis\u001b[39;00m\n\u001b[0;32m   1187\u001b[0m     axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maxis \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n",
      "File \u001b[1;32mC:\\Anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1690\u001b[0m, in \u001b[0;36m_iLocIndexer._getitem_tuple\u001b[1;34m(self, tup)\u001b[0m\n\u001b[0;32m   1689\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_getitem_tuple\u001b[39m(\u001b[38;5;28mself\u001b[39m, tup: \u001b[38;5;28mtuple\u001b[39m):\n\u001b[1;32m-> 1690\u001b[0m     tup \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_tuple_indexer(tup)\n\u001b[0;32m   1691\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m suppress(IndexingError):\n\u001b[0;32m   1692\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_lowerdim(tup)\n",
      "File \u001b[1;32mC:\\Anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:966\u001b[0m, in \u001b[0;36m_LocationIndexer._validate_tuple_indexer\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m    964\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, k \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(key):\n\u001b[0;32m    965\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 966\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(k, i)\n\u001b[0;32m    967\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m    968\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    969\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLocation based indexing can only have \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    970\u001b[0m             \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_valid_types\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m] types\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    971\u001b[0m         ) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n",
      "File \u001b[1;32mC:\\Anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1592\u001b[0m, in \u001b[0;36m_iLocIndexer._validate_key\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1590\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m   1591\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m is_integer(key):\n\u001b[1;32m-> 1592\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_integer(key, axis)\n\u001b[0;32m   1593\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, \u001b[38;5;28mtuple\u001b[39m):\n\u001b[0;32m   1594\u001b[0m     \u001b[38;5;66;03m# a tuple should already have been caught by this point\u001b[39;00m\n\u001b[0;32m   1595\u001b[0m     \u001b[38;5;66;03m# so don't treat a tuple as a valid indexer\u001b[39;00m\n\u001b[0;32m   1596\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m IndexingError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mToo many indexers\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32mC:\\Anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1685\u001b[0m, in \u001b[0;36m_iLocIndexer._validate_integer\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1683\u001b[0m len_axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis(axis))\n\u001b[0;32m   1684\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m key \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m len_axis \u001b[38;5;129;01mor\u001b[39;00m key \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m-\u001b[39mlen_axis:\n\u001b[1;32m-> 1685\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mIndexError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msingle positional indexer is out-of-bounds\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mIndexError\u001b[0m: single positional indexer is out-of-bounds"
     ]
    }
   ],
   "source": [
    "#读取数据文件  highschool.txt  并获取初三（J3）和高一（S1）的成绩数据\n",
    "print('信计221 刘显婷 224180117')\n",
    "import pandas as pd\n",
    "data = pd.read_csv(\"D:/Users/19202/Desktop/highschool.txt\")\n",
    "j3_scores = data.iloc[:, 0]\n",
    "s1_scores = data.iloc[:, 1]\n",
    "#计算 Pearson 相关系数\n",
    "from scipy.stats import pearsonr\n",
    "pearson_corr, _ = pearsonr(j3_scores, s1_scores)\n",
    "print(\"Pearson 相关系数:\", pearson_corr)\n",
    "#计算 Spearman 相关系数\n",
    "from scipy.stats import spearmanr\n",
    "spearman_corr, _ = spearmanr(j3_scores, s1_scores)\n",
    "print(\"Spearman 相关系数:\", spearman_corr)\n",
    "#计算 Kendall τ 相关系数\n",
    "from scipy.stats import kendalltau\n",
    "kendall_tau_corr, _ = kendalltau(j3_scores, s1_scores)\n",
    "print(\"Kendall τ 相关系数:\", kendall_tau_corr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a443cc1d-869d-477c-9255-aae162e6e0d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pearson Correlation: 0.7952877240923681\n",
      "Spearman Correlation: 0.10885954381752701\n",
      "Kendall τ Correlation: 0.6081771720613288\n"
     ]
    }
   ],
   "source": [
    "def pearson_correlation(xs, ys):\n",
    "    n = len(xs)\n",
    "    sum_x = sum(xs)\n",
    "    sum_y = sum(ys)\n",
    "    sum_x_sq = sum(xi**2 for xi in xs)\n",
    "    sum_y_sq = sum(yi**2 for yi in ys)\n",
    "    psum = sum(xi * yi for xi, yi in zip(xs, ys))\n",
    "    num = psum - (sum_x * sum_y / n)\n",
    "    den = ((sum_x_sq - (sum_x**2 / n)) * (sum_y_sq - (sum_y**2 / n)))**0.5\n",
    "    return num / den if den != 0 else 0\n",
    "def spearman_correlation(xs, ys):\n",
    "    x_ranks = rank_data(xs)\n",
    "    y_ranks = rank_data(ys)\n",
    "    return pearson_correlation(x_ranks, y_ranks)\n",
    "def kendall_tau_correlation(xs, ys):\n",
    "    n = len(xs)\n",
    "    concordant = 0\n",
    "    discordant = 0\n",
    "    for i in range(n - 1):\n",
    "        for j in range(i + 1, n):\n",
    "            concordant += (xs[i] - xs[j]) * (ys[i] - ys[j]) > 0\n",
    "            discordant += (xs[i] - xs[j]) * (ys[i] - ys[j]) < 0\n",
    "    return (concordant - discordant) / (concordant + discordant) if concordant + discordant != 0 else 0\n",
    "def rank_data(data):\n",
    "    return sorted(range(len(data)), key=lambda i: data[i])\n",
    "# 从文件中提取J3和S1的成绩\n",
    "def extract_scores(file_content):\n",
    "    lines = file_content.strip().split(\"\\n\")\n",
    "    headers = lines[0].split()\n",
    "    j3_scores = []\n",
    "    s1_scores = []\n",
    "    for line in lines[1:]:\n",
    "        parts = line.split()\n",
    "        j3_scores.append(int(parts[0]))\n",
    "        s1_scores.append(int(parts[1]))\n",
    "    return j3_scores, s1_scores\n",
    "# 读取文件内容\n",
    "with open(\"D:/Users/19202/Desktop/highschool.txt\", \"r\") as file:\n",
    "    file_content = file.read()\n",
    "# 提取成绩\n",
    "j3_scores, s1_scores = extract_scores(file_content)\n",
    "# 计算相关系数\n",
    "pearson_corr = pearson_correlation(j3_scores, s1_scores)\n",
    "spearman_corr = spearman_correlation(j3_scores, s1_scores)\n",
    "kendall_corr = kendall_tau_correlation(j3_scores, s1_scores)\n",
    "# 打印结果\n",
    "print(f\"Pearson Correlation: {pearson_corr}\")\n",
    "print(f\"Spearman Correlation: {spearman_corr}\")\n",
    "print(f\"Kendall τ Correlation: {kendall_corr}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0fe9d56d-8aa0-4345-8012-a62fb709bd4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n"
     ]
    }
   ],
   "source": [
    "print('信计221 刘显婷 224180117')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "770f9e7e-9910-4856-8b49-d49a4fad0db5",
   "metadata": {},
   "outputs": [],
   "source": [
    "w"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
